package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.mapper.NoticeMapper;
import cn.tedu._03vehicle.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/v1/notice/")
@Slf4j
@Tag(name = "03-通知管理模块")
public class NoticeController {
    @Autowired
    private RedisTemplate<String,Serializable> redisTemplate;

    @Autowired
    private NoticeMapper noticeMapper;

    @GetMapping("list")
    @Operation(summary = "资讯列表")
    @ApiOperationSupport(order = 10)
    public JsonResult notices(){
        ValueOperations<String,Serializable> value = redisTemplate.opsForValue();
        Serializable redis_data = value.get("vehicle_index");
        if (redis_data != null){
            log.debug("数据来自Redis数据库...");
            return JsonResult.ok(redis_data);
        }
        List<NoticeListVO> noticeListVOList = noticeMapper.selectNewNotice();
        value.set("vehicle_index", (Serializable) noticeListVOList, 30, TimeUnit.SECONDS);
        log.debug("数据来自Mysql数据库...");
        return JsonResult.ok(noticeListVOList);
    }
}
